<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加菜单</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/css/oksub.css">
    <script type="text/javascript" src="/static/lib/loading/okLoading.js"></script>

    <link type="text/css" rel="stylesheet" href="/static/css/metroStyle.css">
    <link type="text/css" rel="stylesheet" href="/static/css/treeSelectStyle.css">
    <script src="/static/js/jquery-1.11.2.min.js"></script>
    <script type="text/javascript" src="/static/js/jquery.ztree.all.js"></script>
    <script type="text/javascript" src="/static/js/treeSelect.2.0.js"></script>
    <script type="text/javascript" src="/static/js/xm-select.js"></script>
</head>
<body>
<div class="ok-body">
    <!--form表单-->
    <form class="layui-form layui-form-pane ok-form">
        <div class="layui-row layui-col-space10">
            <div class="layui-inline layui-col-md6">
            <label class="layui-form-label">菜单代码<span style="color: red">*</span></label>
            <div class="layui-input-block">
                <input type="text" name="menu_code" id="menu_code" placeholder="请输入菜单代码！唯一" autocomplete="off" class="layui-input" maxlength="16"
                       lay-verify="required">
                <input type="text" name="id" id="id" class="layui-input" style="display: none">
            </div>
        </div>
            <div class="layui-inline layui-col-md6">
            <label class="layui-form-label">菜单名称<span style="color: red">*</span></label>
            <div class="layui-input-block">
                <input type="text" name="tree_name" id="tree_name" placeholder="请输入菜单名称！" autocomplete="off" class="layui-input"
                       lay-verify="required">
            </div>
        </div>
        </div>
        <div class="layui-row layui-col-space10">
            <div class="layui-inline layui-col-md6">
            <label class="layui-form-label">菜单类型<span style="color: red">*</span></label>
                <div class="layui-input-block">
                    <input type="radio" name="menu_marital" lay-filter="href" value="01"  title="目录">
                    <input type="radio" name="menu_marital" lay-filter="href" value="02" checked  title="菜单">
                    <input type="radio" name="menu_marital" lay-filter="href" value="03"  title="按钮">
                </div>
            </div>
            <div class="layui-inline layui-col-md6">
            <label class="layui-form-label">显/隐<span style="color: red">*</span></label>
            <div class="layui-input-block" id="LAY-switch-check">
                <input type="checkbox" id="now_status" name="now_status" lay-skin="switch" lay-text="显示|隐藏" lay-filter="switchTest" checked="">
            </div>
            </div>
        </div>
        <div class="layui-row layui-col-space10">
            <div class="layui-inline layui-col-md6">
            <label class="layui-form-label">菜单指向<span style="color: red">*</span></label>
            <div class="layui-input-block">
                <input type="text" name="href" id="href" placeholder="请输入菜单指向" autocomplete="off" class="layui-input"
                       lay-verify="required">
            </div>
        </div>
        <div class="layui-inline layui-col-md6">
            <label class="layui-form-label">上级菜单</label>
             <div class="layui-input-block" name="menu_pname" id="menu_pname">

             </div>
        </div>
        </div>
        <div class="layui-row layui-col-space10">
            <div class="layui-inline layui-col-md6">
                <label class="layui-form-label">icon类型<span style="color: red">*</span></label>
                <div class="layui-input-block">
                    <select name="fontFamily" id="fontFamily" lay-filter="aihao">
                        <option value="ok-icon" selected="">ok-icon(推荐)</option>
                        <option value="layui-icon">layui-icon</option>
                    </select>
                </div>
            </div>
        <div class="layui-inline layui-col-md6">
            <label class="layui-form-label">icon图标<span style="color: red">*</span></label>
             <div class="layui-input-block">
           <input type="text" name="icon" id="icon" placeholder="请输入icon图标(unicode)" autocomplete="off" class="layui-input"
                       lay-verify="required">
             </div>
        </div>
        </div>

        <div class="layui-form-item layui-form-text" style="padding-top: 10px;">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark_describe" placeholder="请输入备注内容" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="edit">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary" id="close">关闭</button>
            </div>
        </div>
    </form>
</div>
<!--js逻辑-->
<script src="/static/lib/layui/layui.js"></script>
<script>
    let data1;
    function child(d) {
        data1 = d;

    layui.use(["element","form", "okUtils", "okLayer"], function () {
        let $ = layui.$;
        let form = layui.form;
        let okUtils = layui.okUtils;
        let okLayer = layui.okLayer;
        let tree = layui.tree;
        okLoading.close();
        $('#id').val(data1.id);
        $('#menu_code').val(data1.menu_code);
        $('#tree_name').val(data1.tree_name);
        $('#href').val(data1.href);
        $('#menu_pid').val(data1.menu_pid);
        $('#menu_pname').val(data1.menu_pname);
        $('#icon').val(data1.icon);
        $('#remark_describe').val(data1.remark_describe);
        $('#now_status').val(data1.now_status);
        if(data1.now_status===0){
            layui.jquery('input[name="now_status"]').attr('checked', 'checked');
            layui.jquery('input[name="now_status"]').next().addClass('layui-form-onswitch'); //开
        }else{
            layui.jquery('input[name="now_status"]').removeAttr('checked');
            layui.jquery('input[name="now_status"]').next().removeClass('layui-form-onswitch'); //关
        }
        // 监听switch状态
        form.on("switch(switchTest)", function (obj) {
             $(obj.elem).attr('type', 'hidden').val(this.checked ? 0 : 1);
             form.render();
        });
        if(data1.menu_marital==='01'){
            $('div.layui-unselect.layui-form-radio')[0].click();
        }else if(data1.menu_marital==='02'){
             $('div.layui-unselect.layui-form-radio')[1].click();
        }else{
             $('div.layui-unselect.layui-form-radio')[2].click();
        }
        // 回显下拉框
        var initvalue;
        if(data1.menu_pid===null){
            initvalue=0;
        }else{
            initvalue=data1.menu_pid
        }
         var demo3 = xmSelect.render({
                el: '#menu_pname',
                model: {label: {type: 'text'}},
                radio: true,
                clickClose: true,
                initValue:[initvalue],
                prop:{
	                name:'tree_name',
                    value:'id',
                },
                tree: {
                    show: true,
                    strict: false,
                    expandedKeys: [-1],
                },
                height: 'auto',
                data() {
                    return []
                }
            });
            $.ajax({
                url: "{% url 'config:menus_list2' %}",
                type: "GET",
                success: function (ret) {
                    var node = ret.data;
                    demo3.update({
                        data: node,
                        autoRow: true,
                    })
                },
                error: function () {
                }
            });

        // 修改动作
        form.on("submit(edit)", function (data) {
            if (data.field.select == data1.id) {
                okLayer.redCrossMsg('不能选择自己为上级单位');
                $('#menu_pname').css('border','1px solid red');
                return false;
            }
            okUtils.ajax("/config/menus_edit/" + data.field.id, "post", data.field, true).done(function (response) {
                okLayer.greenTickMsg(response.msg, function () {
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                    window.parent.location.reload();
                });
            }).fail(function (error) {
                console.log(error)
            });
            return false;
        });
        $("#close").click(function () {
            let index = parent.layer.getFrameIndex(window.name); //获取当前窗口的name
            window.parent.location.reload();
            parent.layer.close(index);

        });
    })};
</script>
</body>
</html>
